package com.zhu.esyblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhu.esyblog.Utils.BeanCopyUtils;
import com.zhu.esyblog.pojo.RespBean;
import com.zhu.esyblog.pojo.SysUser;
import com.zhu.esyblog.mapper.SysUserMapper;
import com.zhu.esyblog.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.esyblog.service.LoginService;
import com.zhu.esyblog.vo.LoginUserVo;
import com.zhu.esyblog.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stx
 * @since 2022-06-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private  SysUserMapper userMapper;

    @Autowired
    private LoginService loginService;

    /**
     * 根据id查询UserVo
     * @param id
     * @return
     */
    @Override
    public UserVo findUserVoById(Long id) {
        SysUser user = userMapper.selectById(id);
        if(user==null){
            user=new SysUser();
            user.setNickname("朱");
            user.setId(1L);
            user.setAvatar("static/img/logo.222.jpg");
        }
        UserVo userVo = BeanCopyUtils.copyBean(user, UserVo.class);
        return userVo;
    }

    /**
     * 根据id查询作者
     * @param id
     * @return
     */
    @Override
    public SysUser findUserById(Long id) {
        SysUser sysUser=userMapper.selectById(id);
        if(sysUser==null){
            sysUser=new SysUser();
            sysUser.setNickname("朱");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.eq(SysUser::getPassword,password);
        queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
        queryWrapper.last("limit 1 ");
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据token查询用户信息
     * @param token
     * @return
     */
    @Override
    public RespBean findUserByToken(String token) {
        /**
         * token合法性校验
         * token是否为空，解析是否成功 redis是否存在
         * 如果失败 返回错误
         * 成功返回结果 LoginUserVo
         */
      if(  StringUtils.isEmpty(token)){
          return RespBean.error("token不合法",10003);
      }
        SysUser user= loginService.checkToken(token);
        LoginUserVo loginUserVo = BeanCopyUtils.copyBean(user, LoginUserVo.class);
        return RespBean.success("success",loginUserVo);
    }

    /**
     * 根据账号查询用户
     * @param account
     * @return
     */
    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1 ");
        SysUser user = userMapper.selectOne(queryWrapper);
        return user;
    }
}
